# -*- coding: utf-8 -*-
"""
分辨率设置对话框
用于调整摄像头硬件分辨率
"""

from PyQt5.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QLabel, 
                             QPushButton, QGroupBox, QGridLayout,
                             QSpinBox, QMessageBox)
from PyQt5.QtCore import Qt, pyqtSignal

class ResolutionDialog(QDialog):
    """分辨率设置对话框"""
    
    # 定义信号
    resolution_changed = pyqtSignal(int, int, int)  # width, height, fps
    
    def __init__(self, parent=None, current_width=640, current_height=480, current_fps=30):
        super().__init__(parent)
        self.setWindowTitle("分辨率设置")
        self.setModal(True)
        self.setFixedSize(350, 300)
        
        # 当前设置
        self.current_width = current_width
        self.current_height = current_height
        self.current_fps = int(current_fps)  # 确保fps是整数
        
        # 选择的设置
        self.selected_width = current_width
        self.selected_height = current_height
        self.selected_fps = int(current_fps)  # 确保fps是整数
        
        self.init_ui()
    
    def init_ui(self):
        """初始化UI"""
        layout = QVBoxLayout()
        
        # 标题
        title_label = QLabel("分辨率设置")
        title_label.setStyleSheet("font-weight: bold; font-size: 14px; margin: 10px;")
        layout.addWidget(title_label)
        
        # 当前设置显示
        current_group = QGroupBox("当前设置")
        current_layout = QGridLayout()
        
        current_layout.addWidget(QLabel("当前分辨率:"), 0, 0)
        current_layout.addWidget(QLabel(f"{self.current_width} x {self.current_height}"), 0, 1)
        
        current_layout.addWidget(QLabel("当前帧率:"), 1, 0)
        current_layout.addWidget(QLabel(f"{self.current_fps} FPS"), 1, 1)
        
        current_group.setLayout(current_layout)
        layout.addWidget(current_group)
        
        # 新设置
        new_group = QGroupBox("新设置")
        new_layout = QGridLayout()
        
        # 宽度设置
        new_layout.addWidget(QLabel("宽度:"), 0, 0)
        self.width_spin = QSpinBox()
        self.width_spin.setRange(80, 1920)
        self.width_spin.setValue(self.selected_width)
        self.width_spin.valueChanged.connect(self.on_setting_changed)
        new_layout.addWidget(self.width_spin, 0, 1)
        
        # 高度设置
        new_layout.addWidget(QLabel("高度:"), 1, 0)
        self.height_spin = QSpinBox()
        self.height_spin.setRange(60, 1080)
        self.height_spin.setValue(self.selected_height)
        self.height_spin.valueChanged.connect(self.on_setting_changed)
        new_layout.addWidget(self.height_spin, 1, 1)
        
        # 帧率设置
        new_layout.addWidget(QLabel("帧率:"), 2, 0)
        self.fps_spin = QSpinBox()
        self.fps_spin.setRange(10, 120)
        self.fps_spin.setValue(self.selected_fps)
        self.fps_spin.valueChanged.connect(self.on_setting_changed)
        new_layout.addWidget(self.fps_spin, 2, 1)
        
        new_group.setLayout(new_layout)
        layout.addWidget(new_group)
        
        # 按钮布局
        button_layout = QHBoxLayout()
        
        button_layout.addStretch()
        
        # 确定和取消按钮
        ok_btn = QPushButton("确定")
        ok_btn.clicked.connect(self.accept)
        ok_btn.setDefault(True)
        
        cancel_btn = QPushButton("取消")
        cancel_btn.clicked.connect(self.reject)
        
        button_layout.addWidget(ok_btn)
        button_layout.addWidget(cancel_btn)
        
        layout.addLayout(button_layout)
        self.setLayout(layout)
    
    def on_setting_changed(self):
        """设置改变"""
        self.selected_width = self.width_spin.value()
        self.selected_height = self.height_spin.value()
        self.selected_fps = int(self.fps_spin.value())  # 确保fps是整数
    
    def get_selected_settings(self):
        """获取选择的设置"""
        return self.selected_width, self.selected_height, self.selected_fps
    
    def accept(self):
        """确认设置"""
        # 发送设置改变信号
        self.resolution_changed.emit(self.selected_width, self.selected_height, self.selected_fps)
        super().accept()
    
    @staticmethod
    def select_resolution(parent=None, current_width=640, current_height=480, current_fps=30):
        """静态方法：显示分辨率设置对话框并返回选择的设置"""
        dialog = ResolutionDialog(parent, current_width, current_height, current_fps)
        if dialog.exec_() == QDialog.Accepted:
            return dialog.get_selected_settings()
        return None, None, None 